<template>
  <div style="position:relative">
    <el-upload
      action="#"
      list-type="picture-card"
      :on-preview="preview"
      :file-list="filelist"
      :on-remove="remove"
      :on-change="change"
      :http-request="upload"
      :before-upload="beforeUpload"
      :class="{ disable: filelist.length > 0 }"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <!-- <transition name="fade">
      <el-progress v-show="showProgress" type="circle" :percentage="persentTag" class="progress" />
    </transition> -->

    <el-dialog title="img" :visible="showDialog" @close="showDialog = false">
      <el-row type="flex" justify="center">
        <img :src="fileUrl" alt="">
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
export default {
  data() {
    return {
      filelist: [],
      showDialog: false,
      fileUrl: ''
      // showProgress: false
      // persentTag: 0
    }
  },
  methods: {
    preview(file) {
      // console.log(file)
      this.showDialog = true
      this.fileUrl = file.url
    },
    remove(file, fileList) {
      // console.log(file)
      this.filelist = [...fileList]
    },
    change(file, fileList) {
      // console.log(file)
      this.filelist = [...fileList]
    },
    beforeUpload(file) {
      console.log(file)
      const imgTypeArr = ['image/jpeg', 'image/png']
      const maxSize = 200 * 1024 * 1024
      if (!imgTypeArr.includes(file.type)) {
        this.$message.error('图片仅支持 .jpeg .png 类型')
        return false
      } else if (file.size > maxSize) {
        this.$message.error('图片最大仅支持 ' + maxSize / 1024 + ' kb')
        return false
      }
      // setTimeout(() => {
      //   this.showProgress = true
      // }, 1000)
    },
    upload(data) {
      console.log(data)
      // console.log(data.size)
      const cos = new COS({
        SecretId: 'AKIDLTLwOaV9WdU4NPAeroVv60d9UbU7UeaG', // 身份识别 ID
        SecretKey: 'RbU6RVIo32kBLiodCUVFjwsXw4uTlmWz' // 身份密钥
      })

      cos.putObject({
        Bucket: 'woocyouth-1305899987', /* 必须 */
        Region: 'ap-nanjing', /* 存储桶所在地域，必须字段 */
        Key: data.file.size + data.file.name, /* 必须 */
        StorageClass: 'STANDARD',
        Body: data.file, // 上传文件对象
        onProgress: (progressData) => {
          // console.log(JSON.stringify(progressData))
          // this.persentTag = parseInt(progressData.percent * 100)
          data.file.percent = parseInt(progressData.percent * 100)
          data.onProgress(data.file)
        }
      }, (err, data) => {
        // console.log(err || data)
        // console.log(data.Location)
        if (!err) {
          this.filelist[0].url = 'http://' + data.Location
          this.filelist[0].status = 'success'
        }

        // setTimeout(() => {
        //   this.showProgress = false
        // }, 200)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .disable {
  .el-upload--picture-card {
    display: none;
  }
}

::v-deep img {
  object-fit: cover;
}

// .progress{
//   position: absolute;
//   z-index: 100;
//   top: 0;
//   left: 0;
//   background-color: rgba(255, 255, 255, 0.863);
// }

// ::v-deep .el-progress-circle{
//   width: 148px !important;
//   height: 148px !important;
// }
// .fade-enter-active, .fade-leave-active {
//       transition: opacity .5s
// }
// .fade-enter, .fade-leave-active {
//       opacity: 0
// }
</style>
